const util = require('../../../utils/util.js');
const api = require('../../../utils/api.js');

Page({
  data: {
    tabs: ['全部', '待支付', '进行中', '已完成', '已取消'],
    activeTab: 0,
    orderList: []
  },

  onLoad(options) {
    // 如果从其他页面传入状态，则切换到对应tab
    if (options.status) {
      this.setData({
        activeTab: parseInt(options.status)
      });
    }
    this.loadOrderList();
  },

  onTabChange(e) {
    const index = e.currentTarget.dataset.index;
    this.setData({
      activeTab: index
    });
    this.loadOrderList();
  },

  loadOrderList() {
    util.showLoading();
    
    // 模拟数据，实际应该从API获取
    setTimeout(() => {
      const mockOrders = [
        {
          id: 1,
          orderNo: 'ORD20231103001',
          serviceName: '居家护理服务',
          image: 'https://dummyimage.com/320x320/4A90E2/fff.png&text=护理',
          price: 120,
          bookingTime: '2023-11-05 14:00',
          createTime: '2023-11-03 10:30',
          status: 0,
          statusText: '待支付',
          statusClass: 'pending'
        },
        {
          id: 2,
          orderNo: 'ORD20231102001',
          serviceName: '家政清洁服务',
          image: 'https://dummyimage.com/320x320/4CAF50/fff.png&text=家政',
          price: 88,
          bookingTime: '2023-11-04 09:00',
          createTime: '2023-11-02 15:20',
          status: 1,
          statusText: '进行中',
          statusClass: 'processing'
        },
        {
          id: 3,
          orderNo: 'ORD20231101001',
          serviceName: '上门体检服务',
          image: 'https://dummyimage.com/320x320/FF9800/fff.png&text=体检',
          price: 299,
          bookingTime: '2023-11-02 10:00',
          createTime: '2023-11-01 14:10',
          status: 2,
          statusText: '已完成',
          statusClass: 'completed'
        },
        {
          id: 4,
          orderNo: 'ORD20231031001',
          serviceName: '配餐服务',
          image: 'https://dummyimage.com/320x320/FF5722/fff.png&text=配餐',
          price: 35,
          bookingTime: '2023-11-01 11:30',
          createTime: '2023-10-31 16:40',
          status: 2,
          statusText: '已完成',
          statusClass: 'completed'
        }
      ];

      // 根据activeTab筛选订单
      let filteredOrders = mockOrders;
      if (this.data.activeTab > 0) {
        filteredOrders = mockOrders.filter(order => order.status === this.data.activeTab - 1);
      }

      this.setData({
        orderList: filteredOrders
      });
      util.hideLoading();
    }, 500);
  },

  onOrderDetail(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/mine/order/order-detail?id=${id}`
    });
  },

  onCancel(e) {
    const id = e.currentTarget.dataset.id;
    util.showConfirm('确定要取消该订单吗？').then(confirm => {
      if (confirm) {
        util.showToast('订单已取消', 'success');
        this.loadOrderList();
      }
    });
  },

  onPay(e) {
    const id = e.currentTarget.dataset.id;
    util.showToast('跳转支付页面');
    // 实际应跳转到支付页面
    // wx.navigateTo({
    //   url: `/pages/payment/payment?orderId=${id}`
    // });
  },

  onEvaluate(e) {
    const id = e.currentTarget.dataset.id;
    util.showToast('跳转评价页面');
    // 实际应跳转到评价页面
  },

  onAgainOrder(e) {
    const id = e.currentTarget.dataset.id;
    util.showToast('再来一单');
    // 实际应跳转到服务详情页
  }
})

